package com.fx.controller;

import com.fx.pojo.IotBean;
import com.fx.service.ClientService;
import com.fx.service.IotService;
import com.fx.service.impl.IotServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.StandardCharsets;


/**
 * @author: 梁峰源
 * @date: 2021/10/11 17:08
 * Description:
 */
@Controller
public class DeviceController {
    Logger logger = LoggerFactory.getLogger(getClass());
/*    //数据库连接对象
    IotService iotService=new IotServiceImpl();*/
    @Autowired
    public ClientService clientService;
/*    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;*/

/*    @GetMapping("/RefreshDataBaseMessage")
    @ResponseBody
    public String RefreshDataBaseMessage(String message){
        if("".equals(message)){
            iotService.QueryAll()
                    .stream()
                    .


        }

        return "success";
    }*/

    /**
     * 风扇一
     * @param message
     */
    //@MessageMapping("/fengshanOpen")        //隐藏前缀“app” ，/app/fengshanOpen
    @GetMapping("/fengshanOpen")
    @ResponseBody
    public String greeting(String message) {
        logger.info("------风扇一Hwche0106fan1-------"+message);
        String order = "Hwche0106fan1" +message+ "T";
        AsynchronousSocketChannel channel = clientService.getChannel();
        channel.write(ByteBuffer.wrap(order.getBytes(StandardCharsets.UTF_8)));     //往服务端发送控制命令数据
        return "success";
    }

    /**
     * 风扇二
     * @param message web端发送过来的消息
     * @return
     */
    @GetMapping("/open_method2")
    @ResponseBody
    public String open_method2(String message){
        logger.info("-----风扇二Hwche0107fan2--------"+message);
        String order = "Hwche0107fan2" +message+ "T";
        AsynchronousSocketChannel channel = clientService.getChannel();
        channel.write(ByteBuffer.wrap(order.getBytes(StandardCharsets.UTF_8)));     //往服务端发送控制命令数据
        return "success";
    }

    /**
     * 步进电机
     * @param message 步进电机的状态，有三个值
     * @return
     */
    @GetMapping("/open_method3")
    @ResponseBody
    public String open_method3(String message){
        logger.info("-----步进电机Hwcie0206motor--------"+message);
        if("正转".equals(message)){
            message="1";
        }else if("反转".equals(message)){
            message="2";
        }else {
            message="0";
        }
        String order = "Hwcie0206motor" +message+ "T";
        AsynchronousSocketChannel channel = clientService.getChannel();
        channel.write(ByteBuffer.wrap(order.getBytes(StandardCharsets.UTF_8)));     //往服务端发送控制命令数据
        return "success";
    }

    /**
     * 可调灯
     * @param message web端发送过来的消息
     * @return
     */
    @GetMapping("/open_method4")
    @ResponseBody
    public String open_method4(String message){
        logger.info("-----可调灯Hwcie0206light--------"+message);
        message= message.equals("on") ? "1":"0";
        String order = "Hwcie0206light" +message+ "T";
        AsynchronousSocketChannel channel = clientService.getChannel();
        channel.write(ByteBuffer.wrap(order.getBytes(StandardCharsets.UTF_8)));     //往服务端发送控制命令数据
        return "success";
    }

    /**
     *自动控制
     * @param message web端发送过来的消息
     * @return
     */
    @GetMapping("/open_method5")
    @ResponseBody
    public String open_method5(String message) throws InterruptedException {
        logger.info("-----自动控制Hwche0105auto--------"+message);
        message= message.equals("on") ? "1":"0";
        String order = "Hwche0105auto" +message+ "T";
        AsynchronousSocketChannel channel = clientService.getChannel();
        channel.write(ByteBuffer.wrap(order.getBytes(StandardCharsets.UTF_8)));     //往服务端发送控制命令数据
        //发送第二条数据
        Thread.sleep(1000);
        order="Hwcie0205auto" +message+ "T";
        channel.write(ByteBuffer.wrap(order.getBytes(StandardCharsets.UTF_8)));     //往服务端发送控制命令数据
        return "success";
    }

    /**
     * 用来发送三个阀值
     * @param message 温度预警阀值1,2和光照预警阀值
     * @return success成功
     */
    @GetMapping("/open_method6")
    @ResponseBody
    public String open_method6(String message) throws InterruptedException {
        //两个温度阈值
        String order1 = "Hwche0107maxa+" +message.split("&")[0]+ "T";
        logger.info("-----用来发送三个阀值open_method6--------"+order1);
        //光照阈值，数值必须要有六位
        String str=message.split("&")[1];
        String order2 = "Hwcie0209max" +str+ "T";
        int cnt=str.toCharArray().length;
        logger.info("-----cnt长度===--------"+cnt);
        if(cnt<6){
            for(int i=0;i<6-cnt;i++){
                str="0" + str;
            }
        }
        order2="Hwcie0209max" +str+ "T";
        logger.info("-----用来发送三个阀值open_method6=====--------"+order2);
        AsynchronousSocketChannel channel = clientService.getChannel();
        channel.write(ByteBuffer.wrap(order1.getBytes(StandardCharsets.UTF_8)));  //往服务端发送控制命令数据
        //两条命令间隔一段时间
        Thread.sleep(1000);
        channel.write(ByteBuffer.wrap(order2.getBytes(StandardCharsets.UTF_8)));  //往服务端发送控制命令数据
        return "success";
    }

    /**
     * 等页面加载完毕后，先向服务器请求一次数据
     * @param message
     * @return
     */
    @GetMapping("/open_method7")
    @ResponseBody
    public String open_method7(String message){
        logger.info("-----open_method6--------"+message);
        String order = "Hwche0103ask" +message+ "T";
        AsynchronousSocketChannel channel = clientService.getChannel();
        channel.write(ByteBuffer.wrap(order.getBytes(StandardCharsets.UTF_8)));     //往服务端发送控制命令数据
        return "success";
    }
}
